<?php
namespace Core;

/**
 *
 * @author Administrator
 *        
 */
class MakeClass
{

    private static $instance;

    private $classCase = array();

    // 初始化
    private function init ()
    {
        // 初始化项目
        bulidApp();
        $this->classCase = getParam();
        if(file_exists(APP_CONFIG.DS."config.php")){
            $array = require APP_CONFIG.DS."config.php";//载入项目配置文件
            if(!defined('STYLE_SKIN')){
                define('STYLE_SKIN', $array['default_skin'].DS);
            }
        }        
    }
    // 执行
    private function exec ()
    {
        foreach ($this->classCase as $model => $action) {
            $className = ucfirst($model) . "Controller";
            require APP_CONTROL . DS . $className . '.php';
            $className = "controller\\" . $className;
            $class = new $className();
            $class->$action();
        }
    }
    // 运行
    private function run ()
    {
        $this->init();
        $this->exec();
    }
    // 单列执行
    public static function appInitialize ()
    {
        if (! isset(self::$instance)) {
            self::$instance = new MakeClass();
        }
        self::$instance->run();
    }

//     public function runs ()
//     {
//         $this->route();
//         $control = $_GET[MODEL_NAME]; // 控制器
//         $action = $_GET[ACTION_NAME]; // 操作函数
//         $controlFile = APP_CONTROL . "/" . $control . "Controller.php";
//         if (! file_exists($controlFile)) {
//             halt("控制器文件不存在" . $controlFile);
//         }
//         include_once $controlFile;
//         $class = "controller\\" .ucwords($control).'Controller';
//         if (! class_exists($class)) {
//             halt($controlFile . "中未定义的类" . $class);
//         }
//         $instance = new $class();
//         if (! method_exists($instance, $action)) {
//             halt($controlFile . "中定义的类" . $class . "没有找到该方法" . $action);
//         }
//         $instance->$action();
//     }

//     public function route ()
//     {
//         $config =array(
//                 'url_mode' => iniGet('url_mode'),
//         );
//         // 两种模式来判断URL格式
//         if ($config['url_mode'] == 1) {
//             // url: http://www.demo.com/mvc/index.php?m=Index&a=index
//             $control = ! empty($_GET[MODEL_NAME]) ? trim($_GET[MODEL_NAME]) : "";
//             $action = ! empty($_GET[ACTION_NAME]) ? trim($_GET[ACTION_NAME]) : "";
//         } else 
//             if ($config['url_mode'] == 2) {
//                 // url: http://www.demo.com/mvc/index.php/Index/index
//                 if (isset($_SERVER['PATH_INFO'])) {
//                     $paths = explode("/", trim($_SERVER['PATH_INFO'], "/"));
//                     $control = array_shift($paths);
//                     $action = array_shift($paths);
//                 }
//             }
//         $_GET[MODEL_NAME] = $control = ! empty($control) ? $control : DEFAULT_MODEL;
//         $_GET[ACTION_NAME] = $action = ! empty($action) ? $action : DEFAUTL_ACTION;
//     }
}

//end Core/MakeClass.php